package org.ucn.dmaa0914.gruppe6.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.ucn.dmaa0914.gruppe6.Customer;
import org.ucn.dmaa0914.gruppe6.CustomerCtr;
import org.ucn.dmaa0914.gruppe6.Item;
import org.ucn.dmaa0914.gruppe6.ItemCtr;
import org.ucn.dmaa0914.gruppe6.Order;
import org.ucn.dmaa0914.gruppe6.OrderCtr;
import org.ucn.dmaa0914.gruppe6.tests.HardCodedObjects;

public class MainWindow {

	private static MainWindow instance = null;
	
	public static MainWindow getInstance()
	{
		if( instance == null )
			instance = new MainWindow();
		
		return instance;
	}
	private JFrame frame;
	private SearchView<Customer> customerView;
	private SearchView<Order> orderView;
	private SearchView<Item> itemView;
	private OrderView orderEditor;
	private CustomerView customerInfo;
	private ItemView itemInfo;

	public JFrame getFrame() {
		return frame;
	}

	/**
	 * Launch the application.
	 * @wbp.parser.entryPoint
	 *
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					instance = window;
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainWindow() {
		HardCodedObjects.addToControllers();
		initialize();
	}

	public void updateViews()
	{
		customerInfo.setCustomer(CustomerCtr.getInstance().getCurrentCustomer());
		itemInfo.setItem(ItemCtr.getInstance().getCurrentItem());
		
		orderView.updateSearchResults();
		itemView.updateSearchResults();
		customerView.updateSearchResults();
		orderEditor.setOrder(OrderCtr.getInstance().getCurrentOrder());
	}
	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 1040, 619);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		TopBar topBar = new TopBar();
		frame.getContentPane().add(topBar, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(BorderFactory.createEtchedBorder());
		panel.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		customerInfo = new CustomerView();
		panel_2.add(customerInfo, BorderLayout.SOUTH);
		
		JLabel lblOrders = new JLabel("Customers");
		panel_2.add(lblOrders, BorderLayout.NORTH);
		lblOrders.setBackground(UIManager.getColor("Button.background"));
		lblOrders.setHorizontalAlignment(SwingConstants.CENTER);
		
		customerView = new SearchView<Customer>(CustomerCtr.getInstance());
		lblOrders.setLabelFor(customerView);
		panel_2.add(customerView);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(BorderFactory.createEtchedBorder());
		panel.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		orderView = new SearchView<Order>(OrderCtr.getInstance());
		panel_4.add(orderView, BorderLayout.CENTER);
		
		JLabel lblCustomers = new JLabel("Orders");
		lblCustomers.setHorizontalAlignment(SwingConstants.CENTER);
		orderView.add(lblCustomers, BorderLayout.NORTH);
		
		JPanel panel_5 = new JPanel();
		orderView.add(panel_5, BorderLayout.SOUTH);
		
		JButton btnNew = new JButton("New");
		panel_5.add(btnNew);
		
		JButton btnDelete = new JButton("Delete");
		panel_5.add(btnDelete);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new GridLayout(1, 0, 0, 0));
		
		JPanel panel_6 = new JPanel();
		panel_6.setBorder(BorderFactory.createEtchedBorder());
		panel_1.add(panel_6);
		panel_6.setLayout(new BorderLayout(0, 0));
		
		itemView = new SearchView<Item>(ItemCtr.getInstance());
		panel_6.add(itemView, BorderLayout.CENTER);
		
		itemInfo = new ItemView();
		itemView.add(itemInfo, BorderLayout.SOUTH);
		
		JLabel lblItems = new JLabel("Items");
		lblItems.setHorizontalAlignment(SwingConstants.CENTER);
		panel_6.add(lblItems, BorderLayout.NORTH);
		
		orderEditor = new OrderView();
		frame.getContentPane().add(orderEditor, BorderLayout.CENTER);
		
		// Event handlers
		
		// Handle when current customer should be set
		customerView.getList().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int id = 0;
				if( customerView.getList().getSelectedValue() != null )
				{
					id = customerView.getList().getSelectedValue().getId();
					CustomerCtr.getInstance().setCurrentCustomer(id);
					customerInfo.setCustomer(id);
					orderView.updateSearchResults();
				}

				
			}
		});
		
		itemView.getList().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int id = 0;
				if( itemView.getList().getSelectedValue() != null )
				{
					id = itemView.getList().getSelectedValue().getId();
					ItemCtr.getInstance().setCurrentItem(id);
					itemInfo.setItem(id);
				}

				
			}
		});
		
		orderView.getList().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				
				if( orderView.getList().getSelectedValue() != null )
				{
					OrderCtr.getInstance().setCurrentOrder(orderView.getList().getSelectedValue().getId());
					orderEditor.setOrder(orderView.getList().getSelectedValue().getId());
				}
				
			}
		});
	}

}
